package com.xinsoft.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.CusPatrolStandardRefParam;
import com.xinsoft.entity.po.CusDevicePatrolStandardRef;
import com.xinsoft.service.CusDevicePatrolStandardRefService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName CusDeviceStanrdRefController
 * @Description 设备巡检标准
 * @Author lijie
 * @Date 2023-6-13 12:53:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/cus/device/standardRef")
@Api(value = "CusDeviceStanrdRefController", tags = {"设备巡检标准控制层"})
public class CusDeviceStanrdRefController {
	@Autowired
	private CusDevicePatrolStandardRefService refService;


	/**
	 * @Author Li9527
	 * @Description 设备巡检标准-新增
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("新增")
	@PostMapping("/add")
	public Response<?> add(@RequestBody List<CusDevicePatrolStandardRef> refList) {
		// 参数校验
		for (int i = 0; i < refList.size(); i++) {
			CusDevicePatrolStandardRef oldObj = refService.getOne(new QueryWrapper<CusDevicePatrolStandardRef>().eq(
					"device_id",refList.get(i).getDeviceId())
					.eq("patrol_standard_id",refList.get(i).getPatrolStandardId()));
			if(oldObj != null){
				return Response.fail("集合中存在已重复设备巡检标准,请不要重复添加");
			}
		}
		return Response.succeed(refService.saveBatch(refList));
	}


	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		refService.removeByIds(ids);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 设备巡检标准控分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/pageList")
	public Response<?> pageList(@Valid @RequestBody CusPatrolStandardRefParam param) {
		if(param.getDeviceId() == null){
			return Response.fail("设备id必传");
		}
		return Response.succeed(refService.findPageListByParam(param));
	}
}
